<!-- 积分明细->列表 -->
<template>
  <div style="padding: 15px;">
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      show-footer
      highlight-hover-row
      row-id="id"
      class="mytable-footer"
      :export-config='{type:"csv"}'
      height="700"
      align="center"
      :data="tableData"
      remote-sort
      @sort-change="sortList"
      :edit-config="{trigger: 'click', mode: 'row', showStatus: true}"
      :seq-config="{startIndex: listQuery.pageNum * listQuery.pageSize - listQuery.pageSize}"
    >
      <vxe-table-column type="seq" width="60"></vxe-table-column>
      <vxe-table-column type="checkbox" width="60"></vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="name" title="姓名" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.name" placeholder="请输入员工姓名" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="phone" title="手机号" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.phone" placeholder="请输入手机号" clearable/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="gmtCreated"
          show-header-overflow
          show-overflow
          title="变更时间"
          min-width="420"
          sortable
          :resizable="false"
        />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreated"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
      <vxe-table-column field="changeType" title="变更类型" min-width="150" width="150" sortable :formatter="formatterchangeType"/>
      <template v-slot:header="{ column }">
        <el-select v-model="listQuery.changeType" placeholder="请选择" clearable>
          <el-option
              v-for="item in employmentList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
        </el-select>
      </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="changePoints" title="积分变更" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.changePoints" placeholder="请输入积分变更" clearable type='number'/>
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="remainingAvailablePoints" title="变更后可用积分" min-width="200" sortable/>
        <template v-slot:header="{ column }">
          <el-input
            v-model="listQuery.remainingAvailablePointsLow"
            type="number"
            oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
            placeholder="下限"
            clearable
            style="width: 45%"
          />
          -
          <el-input
            type="number"
            oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
            v-model="listQuery.remainingAvailablePointsUp"
            placeholder="上限"
            clearable
            style="width: 45%"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="remarks" title="描述" min-width="150" sortable/>
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.remarks" placeholder="请输入描述" clearable/>
        </template>
      </vxe-table-column>
    </vxe-table>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
<!--    <vxe-modal v-model="modelDetailFlag" title="新增" width="500" height="350" destroy-on-close resize>-->
<!--      <compEdit :auditEdit="compEdit" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" :operationCode="operationCode"/>-->
<!--    </vxe-modal>-->
    <!--    <vxe-modal v-model="modelEditFlag" title="编辑" width="500" height="350" destroy-on-close resize>-->
    <!--      <compEdit ref="editRef" @fatherEditMethod="fatherEdit" @fatherCancelMethod="fatherCancel" :operationCode="operationCode" />-->
    <!--    </vxe-modal>-->
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import XEUtils from 'xe-utils'
import VXEUtils from 'vxe-utils'
Vue.use(VXETable, VXEUtils, XEUtils, {mounts: ['cookie']})
import {pointsItemList} from "@/api/integralManage/integralDetail";
export default {
  name: 'Parent',
  components: {

  },
  data() {
    return {
      message: '',
      loading: false,
      tableData: [],
      modelAddFlag: false,
      modelEditFlag: false,
      modelDetailFlag: false,
      gmtCreated:"",
      employeePoints:"",
      copy: false,
      listQuery: {
        name: '',
        phone: '',
        gmtCreated:'',
        changeType: '',
        changePoints: '',
        remainingAvailablePoints:'',
        remarks:'',
        pageNum: 1,
        pageSize: 10,
        sortField: '',
        sortType: '',
        total: 0
      },
      employmentList: [
        { value: 'pack', label: '完成打包任务' },
        { value: 'paste', label: '完成贴单任务' },
        { value: 'pack_paste', label: '完成打包和贴单任务' },
        { value: 'reward', label: '奖励积分' },
        { value: 'exchange',  label: '积分兑换' }
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
    }
  },
  created() {
    this.findList()
  },
  methods: {
    findList() {
      this.loading = true
      pointsItemList(this.listQuery).then(response => {
        this.tableData = response.data.records
        this.listQuery.total = response.data.total
        this.loading = false
      })
    },
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 排序
    sortList({column, property, order}) {
      if (order) {
        this.listQuery.sortField = property
        this.listQuery.sortType = order
      } else {
        this.listQuery.sortField = ''
      }
      this.findList()
    },
    // 修改方法
    funcEdit(code) {
      this.auditEdit = code
      this.modelEditFlag = true
    },
    // 详情方法
    funcDetail(code) {
      this.supplyDetail = code
      this.modelDetailFlag = true
    },
    // 增加组件的父方法
    fatherAdd() {
      this.modelAddFlag = false
      this.findList()
    },
    // 编辑组件的父方法
    fatherEdit() {
      this.modelEditFlag = false
      this.findList()
    },
    // 公共取消
    fatherCancel(modelFlag) {
      switch (modelFlag) {
        case 'add':
          this.modelAddFlag = false
          break;
        case 'edit':
          this.modelEditFlag = false
          break;
        case 'detail':
          this.modelDetailFlag = false
          break;
        default:
      }
    },
    // 分页handle
    handlePageChange({currentPage, pageSize}) {
      this.listQuery.pageNum = currentPage
      this.listQuery.pageSize = pageSize
      this.findList()
    },
    // 底部颜色设置
    footerCellClassName({$rowIndex, column, columnIndex}) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return 'col-blue'
        } else {
          return 'col-red'
        }
      }
    },
    formatterchangeType({ cellValue }) {
      const item = this.employmentList.find(
        item => item.value === cellValue
      );
      return item ? item.label : "";
    },
    // 设置底部合计
    footerMethod({columns, data}) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return '合计'
          }
          if (['pageviews', 'forecast'].includes(column.property)) {
            return XEUtils.sum(data, column.property)
          }
          return null
        })
      ]
    }
  }
}
</script>
